/**
 * 739. 每日温度
 * https://leetcode-cn.com/problems/daily-temperatures/
 */
const { displayBaseArray } = require('../../libs/public');

/**
 * @param {number[]} temperatures
 * @return {number[]}
 */
function dailyTemperatures(temperatures) {
  const stack = new Array(temperatures.length).fill(0);
  const result = new Array(temperatures.length).fill(0);
  let top = -1;
  for (let i = 0; i < temperatures.length; i += 1) {
    while (top >= 0 && temperatures[stack[top]] < temperatures[i]) {
      result[stack[top]] = i - stack[top];
      top -= 1;
    }
    top += 1;
    stack[top] = i;
  }
  while (top >= 0) {
    result[stack[top]] = 0;
    top -= 1;
  }
  return result;
}

console.log(displayBaseArray(dailyTemperatures([73, 74, 75, 71, 69, 72, 76, 73])) === '[1,1,4,2,1,1,0,0]');
console.log(displayBaseArray(dailyTemperatures([30, 40, 50, 60])) === '[1,1,1,0]');
console.log(displayBaseArray(dailyTemperatures([30, 60, 90])) === '[1,1,0]');
